package com.xtaller.easy.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.F;
import com.xtaller.common.kit.TimeKit;
import com.xtaller.easy.admin.base.TServiceImpl;
import com.xtaller.easy.admin.mapper.SysMenuAuthMapper;
import com.xtaller.easy.admin.model.SysMenuAuth;
import com.xtaller.easy.admin.service.ISysMenuAuthService;
import org.springframework.stereotype.Service;

import java.util.List;
import com.xtaller.common.convert.W;
import com.xtaller.common.bean.Where;
import org.springframework.transaction.annotation.Transactional;

/**
* Created by Taller on 2018/04/13
*/
@Service
public class SysMenuAuthService extends TServiceImpl<SysMenuAuthMapper,SysMenuAuth> implements ISysMenuAuthService {
   /**************************CURD begin******************************/ 
   // 创建
   @Override
   public SysMenuAuth createSysMenuAuth(SysMenuAuth model) {
       if(this.insert(model))
           return this.selectById(model.getId());
       return null;
   }
 
   // 删除
   @Override
   public Boolean deleteSysMenuAuth(Object ids,String reviser) {
       return this.delete(ids,reviser);
   }
 
   // 修改
   @Override
   public SysMenuAuth updateSysMenuAuth(SysMenuAuth model) {
       if(this.update(model))
           return this.selectById(model.getId());
       return null;
   }
 
   // 查询
   @Override
   public List<SysMenuAuth> findByIds(Object ids) {
       return this.selectByIds(ids);
   }
 
   // 属于
   @Override
   public Boolean exist(List<Where> w) {
       w.add(new Where("1"));
       return this.query(w).size() > 0;
   }
 
   // 查询一个id是否存在
   @Override
   public Boolean existId(Object id) {
       where = W.f(
               W.and("id","eq",id),
               W.field("1")
       );
       return this.query(where).size() > 0;
   }



    /**************************CURD end********************************/
    @Override
    public Boolean initAuthByMenuId(String reviser, Long reviseTime, String menuId) {
        return baseMapper.initAuthByMenuId(reviser, reviseTime, menuId);
    }

    @Transactional
    @Override
    public Boolean setAuth(List<SysMenuAuth> auths) {
        SysMenuAuth auth = auths.get(0);
        String reviser = auth.getCreator();
        Long reviseTime = TimeKit.getTimestamp();
        String menuId = auth.getMenuId();
        initAuthByMenuId(reviser, reviseTime, menuId);
        for(SysMenuAuth model : auths){
            this.createSysMenuAuth(model);
        }
        return true;
    }

    @Override
    public List<JSONObject> getAuthByMenuId(String menuId) {
        return F.f2l(baseMapper.getAuthByMenuId(menuId),
                "id", "menuId", "buttonId");
    }
}